package com.happyfamily.springboot.crowdsourcingplatform.dao.sun;

import com.happyfamily.springboot.crowdsourcingplatform.model.Alliance;
import com.happyfamily.springboot.crowdsourcingplatform.model.User_alliance;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface AllianceDao
{
	@Select("<script> SELECT * FROM alliance WHERE aid NOT IN" +
			"(SELECT aid FROM user_alliance_relation WHERE uid=#{uid})" +
			"<when test='aname!=null'> AND aname LIKE CONCAT ('%',#{aname},'%')</when>" +
			"LIMIT #{limit} OFFSET #{end} </script>")
	public abstract List<Alliance> getList(Long uid,Integer limit,Integer end,String aname);


	@Select("<script> SELECT COUNT(*) FROM alliance WHERE aid IN" +
			"(SELECT aid FROM user_alliance_relation WHERE uid=#{uid})" +
			"<when test='aname!=null'> AND aname LIKE CONCAT ('%',#{aname},'%')</when> </script>")
	public abstract int getSum(Long uid,String aname);

	@Select("<script> SELECT * FROM alliance WHERE aid IN" +
			"(SELECT aid FROM user_alliance_relation WHERE uid=#{uid})" +
			"<when test='aname!=null'> AND aname LIKE CONCAT ('%',#{aname},'%')</when>" +
			"LIMIT #{limit} OFFSET #{end} </script>")
	public abstract List<Alliance> getList2(Long uid,Integer limit,Integer end,String aname);


	@Select("SELECT * FROM alliance WHERE amasterid=#{uid}")
	public abstract List<Alliance> getMyAlliance(Long uid);

	@Select("<script> SELECT COUNT(*) FROM alliance WHERE aid NOT IN" +
			"(SELECT aid FROM user_alliance_relation WHERE uid=#{uid})" +
			"<when test='aname!=null'> AND aname LIKE CONCAT ('%',#{aname},'%')</when> </script>")
	public abstract int getSum2(Long uid,String aname);


	@Select("SELECT sname FROM server_type WHERE sid=#{sid}")
	public abstract String getServerType(Long sid);

	@Select("SELECT sid FROM server_type WHERE sname=#{sname}")
	public abstract String getServerValue(String sname);

	@Insert("INSERT INTO alliance (aname,ainfo,atype,amargin,aaccout,abank,amasterid) VALUES (#{aname},#{ainfo},#{atype},#{amargin},#{aaccout},#{abank},#{amasterid})")
	@Options(useGeneratedKeys = true, keyProperty = "aid")
	public abstract Long addAlliance(Alliance alliance);

	@Insert("INSERT INTO user_alliance_relation (uid,aid,uastate) VALUES (#{uid},#{aid},#{uastate})")
	public abstract int addUserAlliance(User_alliance ua);

	@Select("SELECT COUNT(*) FROM alliance WHERE amasterid=#{uid} AND aid=#{aid}")
	public abstract int checkAlliance(Long uid,Long aid);

	@Select("SELECT COUNT(*) FROM alliance WHERE aaccout=#{account}")
	public abstract int checkAccount(String account);

	@Delete("DELETE FROM alliance WHERE aid=#{aid}")
	public abstract int deleteAlliance(Long aid);

	@Delete("DELETE FROM user_alliance_relation WHERE aid=#{aid}")
	public abstract int deleteAll(Long aid);

	@Delete("DELETE FROM user_alliance_relation WHERE aid=#{aid} AND uid=#{uid}")
	public abstract int backAlliance(Long aid,Long uid);

	@Select("SELECT * FROM user_alliance_relation WHERE aid=#{aid}")
	public abstract List<User_alliance> getUsers(Long aid);

	@Select("SELECT * FROM user_alliance_relation WHERE aid=#{aid} AND uid=#{uid}")
	public abstract User_alliance getUa(Long aid,Long uid);

	@Update("UPDATE user_alliance_relation SET uastate=#{state} WHERE aid=#{aid} AND uid=#{uid}")
	public abstract int updateState(Long aid,Long uid,String state);

	@Select("SELECT * FROM alliance WHERE aid=#{aid}")
	public abstract Alliance getAlliance(Long aid);
}